package com.itheima.service_20211015_114634.Impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service_20211015_114634.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
    private SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Brand> selectAll() {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        List<Brand> brands = mapper.selectAll();

        //  4.关闭资源
        sqlSession.close();
        return brands;

    }

    /**
     * 添加数据
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        mapper.add(brand);

        //  4.提交事务
        sqlSession.commit();

        //  5.关闭资源
        sqlSession.close();
    }

    /**
     * 数据回显  通过id查找数据
     * @return
     */
    @Override
    public Brand selectById(int id) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        Brand brand = mapper.selectById(id);

        //  4.关闭资源
        sqlSession.close();
        return brand;
    }

    /**
     * 修改数据
     * @param brand
     */
    @Override
    public void Update(Brand brand) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        mapper.Update(brand);

        //  4.提交事务
        sqlSession.commit();

        //  5.关闭资源
        sqlSession.close();
    }

    /**
     * 单个删除
     * @param id
     */
    @Override
    public void deleteById(int id) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        mapper.deleteById(id);

        //  4.提交事务
        sqlSession.commit();

        //  5.关闭资源
        sqlSession.close();
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(int[] ids) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.调用方法
        mapper.deleteByIds(ids);

        //  4.提交事务
        sqlSession.commit();

        //  5.关闭资源
        sqlSession.close();
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.计算开始索引和展示条目每页
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        //  4.调用mapper方法
        List<Brand> rows = mapper.selectByPage(begin,size);
        int totalCount = mapper.selectTotalCount();

        //  5.封装成PageBean对象
        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(rows);
        brandPageBean.setTotalCount(totalCount);

        //  6.关闭资源
        sqlSession.close();
        return brandPageBean;
    }

    /**
     * 分页条件查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //  1.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();

        //  2.获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //  3.计算开始索引和展示条目每页和处理条件参数
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        String brandName = brand.getBrandName();
        if (brandName != null && brandName.length() > 0) {
            brand.setBrandName("%" + brandName + "%");
        }

        String companyName = brand.getCompanyName();
        if (companyName != null && companyName.length() > 0) {
            brand.setCompanyName("%" + companyName + "%");
        }
        //  4.调用mapper方法
        List<Brand> rows = mapper.selectByPageAndCondition(begin,size,brand);
        int totalCount = mapper.selectTotalCountByCondition(brand);

        //  5.封装成PageBean对象
        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(rows);
        brandPageBean.setTotalCount(totalCount);

        //  6.关闭资源
        sqlSession.close();
        return brandPageBean;
    }

}
